;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")

;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
;#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "Iio.lisp")
(require "M-avl-string-keys.lisp")
(require "Mhelpers.lisp")
(require "Mperformance.lisp")
(require "Mvolatility.lisp")
(require "Mvolume.lisp")
(require "Mtradeincrease.lisp")

(module Mi/o
  (import Itree)
  (import Ihelpers)
  (import Iperformance)
  (import Ivolatility)
  (import Ivolume)
  (import Itradeincrease)
  (include-book "io-utilities" :dir :teachpacks)
  (include-book "list-utilities" :dir :teachpacks)
  (set-state-ok t)
  
  ; takes in a line from the input file and a tree and
  ; inserts the stock data into the tree, returning the
  ; new tree
  (defun insert-record (tree line)
    (let* ((date (chrs->str (car line)))
           (ticker (chrs->str (cadr line)))
           (opening-price (str->rat (chrs->str (caddr line))))
           (highest-price (str->rat (chrs->str (cadddr line))))
           (lowest-price (str->rat (chrs->str (car (cddddr line)))))
           (closing-price (str->rat (chrs->str (car (cdr (cddddr line))))))
           (num-traded (str->int (chrs->str (car (cdr (cdr (cddddr line)))))))
           (datalist (list opening-price highest-price lowest-price closing-price num-traded)))
      (if (equal (avl-retrieve tree date) nil)
          (avl-insert tree date (avl-insert (empty-tree) ticker datalist)) ; if the given date doesn't exist, create it in the tree
          (avl-insert tree date (avl-insert (cdr (avl-retrieve tree date)) ticker datalist))))) ; date does exist, add new ticker to its tree
  
  ; helps read-stocks-file to insert the stocks into
  ; an AVL tree and returns the new tree
  (defun read-stocks-file-helper (stocklist)
    (if (consp stocklist)
        (let* ((first (car stocklist))
               (firststock (packets #\, first)))
          (insert-record (read-stocks-file-helper (cdr stocklist)) firststock))
        (empty-tree)))
  
  ; uses read-stocks-file-helper to read the stock
  ; history file and insert each record into an AVL
  ; tree for processing and storage
  (defun read-stocks-file (f-in)
    (let* ((file (car (file->string f-in state)))
           (filechrs (remove '#\return (str->chrs file)))
           (filelist (packets #\newline filechrs)))
      (read-stocks-file-helper filelist)))
  
  ; gets passed in a query as a list of char
  ; lists and the stocks tree and performs the 
  ; specified query, returning the results
  ; as a string
  (defun execute-query (query tree)
    (let* ((type (chrs->str (car query)))
           (start (chrs->str (cadr query)))
           (end (chrs->str (caddr query)))
           (tickers (cdddr query)))
      (cond
       ((equal type "Performance") (concatenate 'string type ", " start ", " end "> " (performance-query start end (chrs-words->strings tickers) tree) "\r\n"))
        ((equal type "Volatility") (concatenate 'string type ", " start ", " end "> " (volatility-query start end (chrs-words->strings tickers) tree) "\r\n"))
        ((equal type "Volume") (concatenate 'string type ", " start ", " end "> " (volume-query start end (chrs-words->strings tickers) tree) "\r\n"))
        ((equal type "Tradeincrease") (concatenate 'string type ", " start ", " end "> " (tradeincrease-query start end (chrs-words->strings tickers) tree) "\r\n")))))
  
  ; helps read-queries-and-execute by taking each
  ; line in the queries file and calling execute-query
  ; on them one-by-one and making a string list
  ; out of all the query results
  (defun read-queries-and-execute-helper (querylist tree)
    (if (consp querylist)
        (let* ((first (car querylist))
               (firstquery (packets #\, first)))
          (cons (execute-query firstquery tree) (read-queries-and-execute-helper (cdr querylist) tree)))
        nil))
  
  ; reads in the queries file as a list of 
  ; lists of characters representing each line
  ; and passes this to the helper to be executed
  (defun read-queries-and-execute (f-in tree)
    (let* ((file (car (file->string f-in state)))
           (filechrs (remove #\return (str->chrs file)))
           (filelist (packets #\newline filechrs)))
      (read-queries-and-execute-helper filelist tree)))
  
  ; main function, calls everything else to read
  ; the input, perform the calculations and 
  ; write the results to the file
  (defun read-files-write-output (f-stocks f-queries f-out)
    (mv-let (error-close state)
            (string-list->file f-out
                               (read-queries-and-execute f-queries (read-stocks-file f-stocks))
                               state)
            (if error-close
                (mv error-close state)
                (mv (string-append "input file: "
                                   (string-append f-stocks
                                                  (string-append ", output file: " f-out)))
                    state))))
  
  (export Ii/o))

(link Rmain
      (import)
      (export Ii/o)
      (Mtree Mhelpers Mstockretrieval Mperformance Mvolatility Mvolume Mtradeincrease Mi/o))

;>>>>>>> .r35
(invoke Rmain)